package com.fenghuajueli.module_user.activity.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.fenghuajueli.lib_data.entity.ThirdLoginInfoEntity;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.lib_data.entity.UserInfoEntity;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.activity.setting.UserSettingActivity;
import com.fenghuajueli.module_user.databinding.ActivityUserInfoBinding;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_user.model.UserViewModel;
import com.umeng.socialize.UMShareAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

@Route(path = UserModuleRoute.USER_INFO_ACTIVITY)
public class UserInfoActivity extends UserViewModelBaseActivity implements View.OnClickListener {

    ActivityUserInfoBinding binding;
    private CommonTipsDialog commonTipsDialog;
    private final UserViewModel userViewModel = new UserViewModel();

    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityUserInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        EventBus.getDefault().register(this);
        initUserInfo();
        setOnClick();
        userViewModel.userInfoGetSuccess.observe(this, new Observer<UserInfoEntity>() {
            @Override
            public void onChanged(UserInfoEntity userInfoEntity) {
                setUserInfo();
            }
        });
    }

    private void initUserInfo() {
        if (UserInfoUtils.getInstance().isLogin()) {
            binding.btnLoginOut.setVisibility(View.VISIBLE);
            binding.btnDeleteUser.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(UserInfoUtils.getInstance().getUserInfoEntity().getToken())) {
                userViewModel.getUserInfo(this, UserInfoUtils.getInstance().getUserInfoEntity().getToken());
            } else {
                setUserInfo();
            }
        } else {
            binding.btnLoginOut.setVisibility(View.GONE);
            binding.btnDeleteUser.setVisibility(View.GONE);
            finish();
        }
    }

    private void setOnClick() {
        binding.tvOpenVip.setOnClickListener(this);
        binding.btnDeleteUser.setOnClickListener(this);
        binding.btnLoginOut.setOnClickListener(this);
    }


    public void showVipLevel(int level) {
        binding.tvVipLevel.setText(UserInfoUtils.getInstance().getVipNameByType());
        binding.tvVipDay.setText(UserInfoUtils.getInstance().getShowVipDaysByType());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(EventEntity eventBusEntity) {
        if (eventBusEntity.getCode() == EventBusConstants.LOGIN_OUT) {
            finish();
        }
        if (eventBusEntity.getCode() == EventBusConstants.DELETE_USER) {
            finish();
        }
        if (eventBusEntity.getCode() == EventBusConstants.UPDATE_USER_INFO) {
            setUserInfo();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvOpenVip) {
            ARouter.getInstance().build(UserModuleRoute.USER_OPEN_VIP_ACTIVITY).navigation();
        } else if (view.getId() == R.id.btnDeleteUser) {
            if (!UserInfoUtils.getInstance().isLogin()) {
                return;
            }
            showDeleteUserTipsDialog();
        } else if (view.getId() == R.id.btnLoginOut) {
            showLoadingDialog("正在退出登录...");
            binding.btnLoginOut.postDelayed(new Runnable() {
                @Override
                public void run() {
                    UserInfoUtils.getInstance().clearLoginInfo();
                    hideLoadingDialog();
                    EventBus.getDefault().post(new EventEntity(EventBusConstants.LOGIN_OUT, null));
                    finish();
                }
            }, 1000);
        }
    }

    private void showDeleteUserTipsDialog() {
        hideCommTipDialog();
        commonTipsDialog = new CommonTipsDialog(this, "1、账号注销不是退出登录，注销后不可恢复。\n" +
                "2、当您选择注销账号时，您的所有私有数据包括会员权限将清除，不可恢复。\n" +
                "3、因您注销账号造成的会员权益及其他损失，由用户个人承担。");
        commonTipsDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCommTipDialog();
            }
        });
        commonTipsDialog.setConfirmListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCommTipDialog();
                if (UserInfoUtils.getInstance().isPhoneNum()) {
                    ARouter.getInstance().build(UserModuleRoute.USER_DELETE_ACCOUNT).navigation();
                } else {
                    List<ThirdLoginInfoEntity> list = UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getLogin_list();
                    //因为目前只有微信登陆  所以只需判断是否有第三方账号的数据即可 阿里云一键登录是会读取手机号保存到手机号字段的
                    if (list != null && list.size() > 0) {
                        showWxLogOutDialog();
                    } else {
                        showToast("账号异常，请联系客服进行注销！");
                    }
                }

            }
        });
        commonTipsDialog.show();
    }


    /**
     * 微信的注销提示
     */
    private void showWxLogOutDialog() {
        hideCommTipDialog();
        commonTipsDialog = new CommonTipsDialog(this,
                "因您正在注销微信登录账号信息，需要再次验证您的微信身份信息，验证成功后将清除微信登录账号数据",
                "取消", "微信验证");
        commonTipsDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCommTipDialog();
            }
        });
        commonTipsDialog.setConfirmListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCommTipDialog();
                //走第三方解绑的接口 当做注销账号
                userViewModel.verificationWxToLogOut(UserInfoActivity.this);
                //userViewModel.deleteThirdAccount(UserInfoActivity.this, UserInfoUtils.getInstance().getUserInfoEntity().getToken());
            }
        });
        commonTipsDialog.show();
    }


    private void hideCommTipDialog() {
        if (commonTipsDialog != null) {
            commonTipsDialog.dismiss();
            commonTipsDialog = null;
        }
    }

    private void setUserInfo() {
        if (UserInfoUtils.getInstance().isLogin()) {
            if (UserInfoUtils.getInstance().getUserInfoEntity() != null) {
                binding.tvUserName.setText(UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getUser_name());
                showVipLevel(UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getVip_info().getVip_type());
                if (SwitchKeyUtils.getPayStatus()) {
//                    if (UserInfoUtils.getInstance().isForeverVip()) {
//                        binding.tvOpenVip.setVisibility(View.GONE);
//                    } else {
//                        binding.tvOpenVip.setVisibility(View.VISIBLE);
//                    }
                    binding.vipDayContainer.setVisibility(View.VISIBLE);
                    binding.vipLevelContainer.setVisibility(View.VISIBLE);
                } else {
                    binding.vipDayContainer.setVisibility(View.GONE);
                    binding.vipLevelContainer.setVisibility(View.GONE);
                }
                binding.tvOpenVip.setVisibility(View.GONE);
                binding.tvShardId.setText(UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getShare_id());
            }
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideCommTipDialog();
        EventBus.getDefault().unregister(this);
    }
}